package com.bls.blsdemo.networkdemo

import android.support.v7.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bls.blsdemo.R
import com.bls.blsdemo.constant.Interface
import com.bls.blsdemo.model.NetWorkDemoRes
import com.bls.lib.ui.activity.BaseActivity
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_net_work.*

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2017/11/10.
 */

class NetWorkActivity : BaseActivity() {

    private var beanList: MutableList<NetWorkDemoRes.DatasBean>? = null
    private var adapter: ListViewAdapter? = null

    override fun getLayoutId(): Int = R.layout.activity_net_work

    override fun initMain() {

    }

    override fun initView() {
        rv_list.layoutManager = LinearLayoutManager(this)
        adapter = ListViewAdapter(beanList)
        rv_list.adapter = adapter
    }

    override fun initListener() {
        adapter?.setOnItemChildClickListener { adapter, view, position -> ToastUtils.showShort(beanList?.get(position)?.pT_Image) }
    }

    override fun initData() {
        presenter.requestNetData(Interface.HomeProductType, null, 1, 0)
    }

    override fun showOnSuccess(response: Response<*>?, requestId: Int) {
        if (requestId == 0) {
            val res = Gson().fromJson(response?.body().toString(), NetWorkDemoRes::class.java)
            beanList = res.datas
            adapter?.setNewData(beanList)
        }
    }

    private class ListViewAdapter(data: MutableList<NetWorkDemoRes.DatasBean>?) : BaseQuickAdapter<NetWorkDemoRes.DatasBean, BaseViewHolder>(R.layout.item_net_work_demo, data) {
        override fun convert(helper: BaseViewHolder?, item: NetWorkDemoRes.DatasBean?) {
            Glide.with(mContext).load(item?.pT_Image).into(helper?.getView(R.id.iv_image))
            helper?.setText(R.id.tv_message, item?.pT_Name)?.addOnClickListener(R.id.ll_root)
        }
    }
}
